//
//  HTStatusPictureView.swift
//  WeiBoDemo
//
//  Created by 郝涛 on 16/4/21.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit
private let cellid = "cellid"
//        cell 间距
private let itemMargin :CGFloat = 5
//匡高
private let itemWH :CGFloat = (SCREENW - 20 - itemMargin*2)/3
class HTStatusPictureView: UICollectionView,UICollectionViewDataSource {

    var pictureInfo:[HTPictureInfo]?{
        didSet{
            //设置大小
//            print(pictureInfo?.count)
            //设置配图的大小
            let size = getPictureViewSize(pictureInfo?.count ?? 0)
            //更改自己的约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            //刷新
            self.reloadData()
        }
    }
    //配图的Size
    func getPictureViewSize(count:Int)->CGSize{
   

        
        //定义列数
        let col = count >= 3 ? 3 :count
        //定义行数
        let row = (count - 1) / 3 + 1
        
        //配图的匡高
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
        return CGSize(width: width, height: height)
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupui()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //设置视图
    func setupui(){
        self.backgroundColor = UIColor.whiteColor()
        dataSource = self
        
        //注册cell 
        registerClass(HTStatusPictureViewCell.self, forCellWithReuseIdentifier: cellid)
        
        
        
        //设置itemSize 
        let  layout = self.collectionViewLayout as!UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
        //设置垂直和水平间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        
    }
    //数据源方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellid, forIndexPath: indexPath)as!HTStatusPictureViewCell
//        cell.backgroundColor = UIColor.cyanColor()
        
        //给cellfuzhi
        cell.pictureInfo = pictureInfo![indexPath.item]
        
        
//        print(cell.pictureInfo)
        return cell
        
    }

}
//自定义cell
class HTStatusPictureViewCell:UICollectionViewCell{
    
    var pictureInfo:HTPictureInfo?{
        didSet{
            BJImageview.sd_setImageWithURL(NSURL(string: pictureInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
            // 动态图片
            if let p = pictureInfo?.thumbnail_pic where p.hasSuffix(".gif") {
                gifImageView.hidden = false
            }else{
                // 不是动态图片
                gifImageView.hidden = true
            }

        }
        
        
    }
   override init(frame: CGRect) {
        super.init(frame: frame)
    setupUI()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    //设置视图
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        
        addSubview(BJImageview)
        addSubview(gifImageView)
        //约束
        BJImageview.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        // 02
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.bottom.equalTo(contentView)
        }
    }
 /// 背景默认图
    private lazy var BJImageview: UIImageView = {
        
       let img = UIImageView(image: UIImage(named: "timeline_card_top_background"))
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
       return img
    }()
    
    // 动态图片logo
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    
}


